﻿var excellistitemid;
var listitemidselected;
var currentWeb;
var currentSite;
var currentListGuid;

function Export() {


    currentListGuid = SP.ListOperation.Selection.getSelectedList();

    var context = SP.ClientContext.get_current();
    currentSite = context.get_site();
    currentWeb = context.get_web();

    var currentList = currentWeb.get_lists().getById(currentListGuid);
    var items = SP.ListOperation.Selection.getSelectedItems();



    var index;
    listitemidselected = '';
    for (index in items) {
        listitemid = currentList.getItemById(items[index].id);
        listitemidselected = listitemidselected + items[index].id + ',';
    }

    context.executeQueryAsync(Function.createDelegate(this, this.exportsuccess), Function.createDelegate(this, this.exportfailed));
}

function exportsuccess() {
    var form = document.createElement("form");
    form.setAttribute("method", "post");

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "IDselected");
    hiddenField.setAttribute("value", listitemidselected);
    form.appendChild(hiddenField);

    var hiddenListGuid = document.createElement("input");
    hiddenListGuid.setAttribute("type", "hidden");
    hiddenListGuid.setAttribute("name", "ListGuid");
    hiddenListGuid.setAttribute("value", currentListGuid);
    form.appendChild(hiddenListGuid);

    var hiddenViewGuid = document.createElement("input");
    hiddenViewGuid.setAttribute("type", "hidden");
    hiddenViewGuid.setAttribute("name", "ViewGuid");
    hiddenViewGuid.setAttribute("value", ctx.view);
    form.appendChild(hiddenViewGuid);

    form.setAttribute("action", ctx.HttpRoot + "/_layouts/15/ExporttoExcelinMacOS/Exporttoexcel.aspx");
    document.body.appendChild(form);
    form.submit();

    SP.UI.Notify.addNotification('Exported');
}

function exportfailed(sender, args) {
    var statusId = SP.UI.Status.addStatus(args.get_message());
    SP.UI.Status.setStatusPriColor(statusId, 'red');
    latestId = statusId;
}

function exporttoexcelenable() {
    return (true);
}